﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace it_security_project.App_Code.BusinessLogicLayer
{
    public class Comment : BusinessObjectBase
    {
        private string _thread;
        private string _username;
        private string _content;

        public int CommentID
        {
            get;
            set;
        }

        public string Thread
        {
            get
            {
                return this._thread;
            }
            set
            {
                this.ValidationErrors.Remove("CommentThread");

                if (String.IsNullOrWhiteSpace(value))
                {
                    this.ValidationErrors.Add("CommentThread", "The comment must be connected to a thread");
                }
                else if (value.Length > 50)
                {
                    this.ValidationErrors.Add("CommentThread", "The thread can be max 50 characters.");
                }
                this._thread = value != null ? value.Trim() : null;
            }
        }

        public string Username
        {
            get
            {
                return this._username;
            }
            set
            {
                this.ValidationErrors.Remove("CommentUser");

                if (String.IsNullOrWhiteSpace(value))
                {
                    this.ValidationErrors.Add("CommentUser", "The comment must be connected to a user");
                }
                else if (value.Length > 30)
                {
                    this.ValidationErrors.Add("CommentUser", "The user can be max 30 characters.");
                }
                this._username = value != null ? value.Trim() : null;
            }
        }

        public string Content
        {
            get
            {
                return this._content;
            }
            set
            {
                this.ValidationErrors.Remove("Comment");

                if (String.IsNullOrWhiteSpace(value))
                {
                    this.ValidationErrors.Add("Comment", "You must have a comment!");
                }
                else if (value.Length > 500)
                {
                    this.ValidationErrors.Add("Comment", "The comment can be max 500 characters.");
                }
                else if(value != Regex.Replace(value, "<.*?>", string.Empty))
                {
                    this.ValidationErrors.Add("Comment", "The comment contains characters that are not allowed");
                }
                this._content = value != null ? value.Trim() : null;
            }
        }

        public Comment()
        {
        }

        public Comment(int commentID, string thread, string username, string content)
        {
            this.CommentID = commentID;
            this.Thread = thread;
            this.Username = username;
            this.Content = content;
        }
    }
}